package com.xudow.app.ui.adapter.user;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.qanda.Question;
import com.xudow.app.R;
import com.xudow.app.util.MessageUtils;

import java.util.List;

import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 我的错题 List Adapter
 *
 * @author lukecheng
 */
public class QuestionsAdapter extends RecyclerView.Adapter {
    private static final String TAG = makeLogTag(QuestionsAdapter.class);

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private Context context;
    private List<Question> questions;

    public QuestionsAdapter(Context context, List<Question> favorites) {
        this.context = context;
        this.questions = favorites;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_list, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        Question question = questions.get(position);

        viewHolder.title.setText(question.getQuestion());
        viewHolder.answerCount.setText(Integer.toString(question.getAnswerCount()));

        String time = MessageUtils.formatTimeStampString(this.context, question.getCreateTime());
        viewHolder.time.setText(time);
    }

    @Override
    public int getItemCount() {
        return questions.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        private LinearLayout root;
        public TextView title;
        public TextView time;
        public TextView answerCount;

        public ViewHolder(View itemView) {
            super(itemView);

            root = (LinearLayout) itemView.findViewById(R.id.root);

            root.setOnClickListener(this);
            root.setOnLongClickListener(this);

            title = (TextView) itemView.findViewById(R.id.title);
            time = (TextView) itemView.findViewById(R.id.time);
            answerCount = (TextView) itemView.findViewById(R.id.answer_count);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(this.getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
